/* ************************************************************************************************************ */
/* ** SERVER ************************************************************************************************** */
/* ************************************************************************************************************ */

if SERVER then

/* Map */
ConCommandMap = {}

/* Player choose a class */
GM.PlayerChooseClass = function( ply, args )

	/* Get the class name */
	local class = args[1]
	
	/* If no class is passed, do nothing */
	if not class then return end
	
	/* Debug output */
	DEBUG:Msg( ply:Nick() .. " chose class '" .. tostring(class) .. "'", "GM.PlayerChooseClass" )
	
	/* Chat output */
	SendUserMessage( "ChatMsg", ply, "#888888 You're now in class #991111 " .. tostring(class) )
end

/* A player was rescued */
GM.PlayerRescued = function( ply, args )

	/* Variables */
	local plyTeam = ply:Team()

	/* Debug output */
	DEBUG:Msg( ply:Nick() .. " entered a rescue zone", "GM.PlayerRescued" )
	
	/* If player is an attacker, do nothing */
	if player.IsAttacker( ply ) then return end
	
	/* Reward the player as a prisonner */
	if player.IsPrisoner( ply ) then
	
		/* Debug output */
		DEBUG:Msg( ply:Nick() .. " escaped", "GM.PlayerRescued" )
		
		/* If an attacker rescued the player then reward him */
		if player.IsAttacker( ply.BodyGuard ) then
		
			/* Debug output */
			DEBUG:Msg( ply.BodyGuard:Nick() .. " rescued " .. ply:Nick(), "GM.PlayerRescued" )
		end
		
	/* Reward the player as a defender only if he has a bodyguard */
	elseif player.IsDefender( ply ) and player.IsAttacker( ply.BodyGuard ) then
		
		/* Debug output */
		DEBUG:Msg( ply:Nick() .. " stole " .. ply.BodyGuard:Nick() .. "'s rescue reward", "GM.PlayerRescued" )
	end
end

/* Change a player's team */
GM.SetPlayerTeam = function( ply, args )

	/* Variables */
	local targetParam	= args[1]
	local teamParam		= args[2]
	local allParam		= args[3]
	
	/* Ensure valid player target */
	if !targetParam then
		
		/* Debug output */
		DEBUG:Msg( "Given player was not valid", "GM.SetPlayerTeam" )
		
		return
	end
	
	/* Ensure player exists */
	local target		= player.GetByNick( targetParam, allParam )
	if !target then
	
		/* Debug output */
		DEBUG:Msg( "No matching player found", "GM.SetPlayerTeam" )
	
		return
	end
	
	/* Check that the team is valid */
	if not team.Valid( teamParam ) then
	
		/* Debug output */
		DEBUG:Msg( "The given team does not exist: " .. tostring( teamParam ), "GM.SetPlayerTeam" )
	
		return
	end
	
	/* Get team info */
	local teamInfo = team.GetInfo( teamParam )
	
	/* If there are multiple players, loop over them */
	if type( target ) == "table" then
	
		/* Loop players */
		for i,p in ipairs(target) do
		
			/* Set player team */
			SetPlayerTeam( p, teamInfo.ID )
		
			/* Debug output */
			DEBUG:Msg( "Changed " .. p:Nick() .. "'s team to '" .. teamInfo.Name .. "'", "GM.SetPlayerTeam" )
		end
		
	/* If there's only one, change his team */
	else
		/* Set player team */
		SetPlayerTeam( target, teamInfo.ID )
			
		/* Debug output */
		DEBUG:Msg( "Changed " .. target:Nick() .. "'s team to '" .. teamInfo.Name .. "'", "GM.SetPlayerTeam" )	
	end
end

/* Spawn a player */
GM.SpawnPlayer = function( ply, args )

	/* Spawn all given players */
	for i,p in ipairs( args ) do
	
		/* Force respawn the player */
		ForceRespawn( ply, true )
	end
end

/* Player logging in */
GM.Login = function( ply, args )

	/* No args, no password, no login */
	if not args or not args[1] then return false end

	/* Get the given password */
	local pwd	= tostring( args[1] )
	
	/* Check the password and log the player if ok */
	ply.Logged = (pwd == "McFly_is_1337")
	
	/* Tell the player if he successfully logged in */
	if ply.Logged then
		ply:ChatPrint( "Login successful" )
	end
end

/* Test - Test UMSG */
GM.TestUMSG = function( ply, args )
	DEBUG:Msg( "Value: " .. tostring( args[1] or "<filled>" ), "GM.TestUMSG" )
	umsg.Start( "TestUMSG", ply )
	umsg.String( tostring( args[1] or "<filled>" ) )
	umsg.End()
end

/* Test - Get UMSG */
GM.GetUMSG = function( ply, args )
	DEBUG:Msg( "Value: " .. tostring( args[1] ), "GM.GetUMSG" )
	umsg.Start( "GetUMSG", ply )
	umsg.String( tostring( args[1] ) )
	umsg.End()
end

/* General concommand function */
GM.PlayerConCommand = function( ply, cmd, args )
	
	/* Variables */
	local str = "Player " .. ply:Nick() .. " ran command " .. cmd .. " with parameters "
	local paramStr = nil
	
	/* Debug output */
	local i = 1
	local param = args[i]
	while param do
		
		/* Init string / add comma */
		if not paramStr then paramStr = ""
		else paramStr = paramStr .. ", " end
		
		/* Add param */
		paramStr = paramStr .. param
		
		/* Prepare next loop */
		i = i + 1
		param = args[i]
	end
	
	/* Debug output */
	DEBUG:Msg( str .. tostring( paramStr ), "GM.PlayerConCommand" )
	
	/* Execute corresponding function */
	if cmd and ConCommandMap[cmd] then
		ConCommandMap[cmd](ply, args)
	end
end

/* Create commands */
concommand.Add( "bel_player_choose_class", GM.PlayerConCommand )
concommand.Add( "bel_player_rescued", GM.PlayerConCommand )
concommand.Add( "bel_set_player_team", GM.PlayerConCommand )
concommand.Add( "bel_spawn_player", GM.PlayerConCommand )
concommand.Add( "bel_login", GM.PlayerConCommand )
concommand.Add( "bel_test_umsg", GM.PlayerConCommand )
concommand.Add( "bel_get_umsg", GM.PlayerConCommand )

/* Create map */
ConCommandMap["bel_player_choose_class"] = GM.PlayerChooseClass
ConCommandMap["bel_player_rescued"] = GM.PlayerRescued
ConCommandMap["bel_set_player_team"] = GM.SetPlayerTeam
ConCommandMap["bel_spawn_player"] = GM.SpawnPlayer
ConCommandMap["bel_login"] = GM.Login
ConCommandMap["bel_test_umsg"] = GM.TestUMSG
ConCommandMap["bel_get_umsg"] = GM.GetUMSG

end

/* ************************************************************************************************************ */
/* ** CLIENT ************************************************************************************************** */
/* ************************************************************************************************************ */

if CLIENT then



end

/* ************************************************************************************************************ */
/* ** SHARED ************************************************************************************************** */
/* ************************************************************************************************************ */
